package com.st.rzs.redis;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.st.rzs.redis.service.impl.RedisClientManager;

public class RedisCache {

	private static final Log _logger = LogFactory.getLog(RedisClientManager.class.getName());

	private static RedisClientManager _manager = new RedisClientManager();

	private RedisCache() {
	}

	public static Object get(String key) {
		return _manager.getObject(String.valueOf(key));
	}

	public static boolean put(String key, Object value) {
		try {
			if (value instanceof String) {
				_manager.setObject(key, value.toString());
			} else if (value instanceof List) {
				_manager.setObject(key, (List<String>) value);
			} else if (value instanceof Set) {
				_manager.setObject(key, (Set<String>) value);
			} else if (value instanceof Map) {
				_manager.setObject(key, (Map<String, String>) value);
			} else {
				_logger.error("Invalid Object Type.");
			}
			TimeUnit.SECONDS.sleep(1);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			_logger.error(e.getMessage());
		}
		return false;
	}

	public static boolean put(String key, Object value, int ttl) {
		try {
			if (value instanceof String) {

			} else if (value instanceof List) {
				_manager.setObject(key, value.toString(), ttl);
			} else if (value instanceof List) {
				_manager.setObject(key, (List<String>) value, ttl);
			} else if (value instanceof Set) {
				_manager.setObject(key, (Set<String>) value, ttl);
			} else if (value instanceof Map) {
				_manager.setObject(key, (Map<String, String>) value, ttl);
			} else {
				_logger.error("Invalid Object Type.");
			}
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public static Object remove(String key) {
		return _manager.removeObject(key);
	}
}
